import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Menu } from '../models/menu.entity';

@Injectable()
export class MenuService {
  constructor(
    @InjectRepository(Menu)
    private menuRepository: Repository<Menu>,
  ) {}

  findAll(): Promise<Menu[]> {
    return this.menuRepository.find();
  }

  findOne(id: number): Promise<Menu | null> {
    return this.menuRepository.findOneBy({ id });
  }

  create(menuData: Partial<Menu>): Promise<Menu> {
    const menu = this.menuRepository.create(menuData);
    return this.menuRepository.save(menu);
  }

  async update(id: number, menuData: Partial<Menu>): Promise<Menu | null> {
    const menu = await this.findOne(id);
    if (!menu) {
      return null;
    }
    this.menuRepository.merge(menu, menuData);
    return this.menuRepository.save(menu);
  }

  async remove(id: number): Promise<Menu | null> {
    const menu = await this.findOne(id);
    if (!menu) {
      return null;
    }
    return this.menuRepository.remove(menu);
  }
}
